package cn.t.core.mode;

import cn.t.core.enums.BaseErrorCause;
import cn.t.core.enums.base.CommonError;
import lombok.Data;

import java.util.Objects;

@Data
public class Result<T> {
	private Integer errCode;
	private String errMsg;
	private T data;
	private Integer code;

	public boolean checkSuccessErrCode() {
		if (!BaseErrorCause.SUCCESS.getCode().equals(this.errCode) || Objects.isNull(this.data)) {
			return false;
		}

		return true;
	}

	public boolean checkSuccessCode200() {
		if (!BaseErrorCause.SUCCESS.getCode().equals(this.code) || Objects.isNull(this.data)) {
			return false;
		}
		return true;
	}

	public boolean checkSuccessCode0() {
		if (this.code != 0 || Objects.isNull(this.data)) {
			return false;
		}
		return true;
	}

	public  static <T> Result<T> success(T data) {
		Result<T> result = new Result<T>();
		result.setCode(BaseErrorCause.SUCCESS.getCode());
		result.setErrCode(BaseErrorCause.SUCCESS.getCode());
		result.setErrMsg(BaseErrorCause.SUCCESS.getMessagePattern());
		result.setData(data);
		return result;
	}

	public  static <T> Result<T> fail(int errCode, String errMsg) {
		Result<T> Result = new Result<T>();
		Result.setCode(errCode);
		Result.setErrCode(errCode);
		Result.setErrMsg(errMsg);
		return Result;
	}

	public  static <T> Result<T> fail(BaseErrorCause cause) {
		Result<T> Result = new Result<T>();
		Result.setErrCode(cause.getCode());
		Result.setCode(cause.getCode());
		Result.setErrMsg(cause.getMessagePattern());
		return Result;
	}

	public  static <T> Result<T> fail(BaseErrorCause cause, String errMsg) {
		Result<T> Result = new Result<T>();
		Result.setErrCode(cause.getCode());
		Result.setCode(cause.getCode());
		Result.setErrMsg(String.format(cause.getMessagePattern(), errMsg));
		return Result;
	}



	public  static <T> Result<T> serverError(String message) {
		Result<T> Result = new Result<T>();
		Result.setErrCode(CommonError.SERVER_ERROR.getCode());
		Result.setCode(CommonError.SERVER_ERROR.getCode());
		Result.setErrMsg(message);
		return Result;
	}
}
